package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.Menu;
import pers.xx.sbm.service.MenuRoleService;
import pers.xx.sbm.service.MenuService;
import pers.xx.sbm.vo.ResResult;

import java.util.List;

/**
 * @author ：xiexing
 * @description：角色授权页面
 * @date ：2020/9/8 14:26
 */
@Controller
@RequestMapping("/role_authorization")
public class RoleAuthorizationController {



    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuRoleService menuRoleService;

    /**
     * @description 跳转到角色授权页面
     * @author XieXing
     * @date 2020/9/8 14:28
     */
    @RequestMapping("/")
    public String roleAuthPate(Model model) {
        Menu menu = new Menu();
        menu.setMenuIsEnable(1);
        List<Menu> menus = menuService.getWrapMenus(menu);
        model.addAttribute("menus", menus);
        return "page/system/role_authorization";
    }

    /**
     * @description 保存授权信息
     * @author XieXing
     * @date 2020/9/8 22:46
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult save(Integer roleId, String menus) {
        return menuRoleService.insertBatch(roleId, menus);
    }

    /**
     * @description 获取菜单数
     * @author XieXing
     * @date 2020/9/17 21:00
     */
    @RequestMapping("/getTreeMenus/{roleId}")
    @ResponseBody
    public List<Menu> getTreeMenus(@PathVariable(value = "roleId") Integer roleId) {
        return menuService.getMenusWithChecked(roleId);
    }


}
